{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>工程师列表</legend>
    </fieldset>
    <blockquote class="layui-elem-quote">
        <button class="layui-btn" data-type="reload" onclick="location.reload()">刷新</button>
        <div class="layui-inline">
            <input class="layui-input" name="key" id="key" placeholder="{:lang('pleaseEnter')}编号或关键字">
        </div>
        <button class="layui-btn" id="search" data-type="reload">{:lang('search')}</button>
        <a href="{:url('addUI')}" class="layui-btn layui-btn-sm">添加工程师</a>
    </blockquote>

    <table class="layui-table" id="list" lay-filter="list"></table>
</div>

{include file="common/foot"/}
<script type="text/html" id="barDemo">
    <a href="{:url('editUI')}?admin_id={{d.admin_id}}" class="layui-btn layui-btn-xs">{:lang('edit')}</a>
    {{# if(d.admin_id==1){ }}
    <a href="#" class="layui-btn layui-btn-xs layui-btn-disabled">{:lang('del')}</a>
    {{# }else{  }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{:lang('del')}</a>
    {{# } }}
    {{# if(d.is_at==1){ }}
    <a href="#" class="layui-btn layui-btn-xs layui-btn-normal" lay-event="resw" title="重置为空闲状态">结束工作</a>
    {{# } }}

    {{# if(d.is_group==0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="gr" ns="1">设为组长</a>
    {{# }else{  }}
    <a class="layui-btn layui-btn-xs" lay-event="gr" ns="0">取消组长</a>
    {{# } }}

</script>
<script type="text/html" id="open">
    {{# if(d.admin_id==1){ }}
    <input type="checkbox" disabled name="is_open" value="{{d.admin_id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="open" checked>
    {{# }else{  }}
    <input type="checkbox" name="is_open" value="{{d.admin_id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="open" {{ d.is_open == 1 ? 'checked' : '' }}>
    {{# } }}
</script>
<script type="text/html" id="com">
    {{# if(d.a_pid&&d.a_pid>=0){ }}
        {{d.p_name}}
    {{# }else{  }}
        平台
    {{# } }}
</script>
<script type="text/html" id="isat">
    {{# if(d.is_at==0){ }}
        空闲
    {{# }else{  }}
        工作中
    {{# } }}
</script>
<script>

    var gid = '{:session("gid")}';
    var wh = {gid:5};
    if(gid=='1'){
        han();
    }else if(gid=='6'){
        wh.apid = '{:session("aid")}';
        wh.is_part = 1;
        han();
    }else{

    }
    var tableIn = null,$ = layui.jquery;

    function han() {
        layui.use(['table','form'], function() {
            var table = layui.table,form = layui.form;
            $ = layui.jquery;
            tableIn = table.render({
                elem: '#list',
                url: '{:url("query")}',
                method:'post',
                cols: [[
                    {field:'username', title: '用户名', width:120,fixed: true}
                    ,{field:'name', title: '姓名', width:130}
                    ,{field:'email', title: '邮箱', width:200}
                    ,{field:'p_name', title: '所属单位', width:200,toolbar:'#com'}
                    ,{field:'title', title: '{:lang("userGroup")}', width:200}
                    ,{field:'tel', title: '{:lang("tel")}', width:150}
                    ,{field:'is_at', title: '工作',width:150,toolbar:'#isat'}
                    ,{field:'is_open', title: '{:lang("status")}',width:150,toolbar: '#open'}
                    ,{width:280, align:'center', toolbar: '#barDemo'}
                ]]
                ,where:wh
                ,page:true
                ,limit:15
            });

            form.on('switch(open)', function(obj){
                loading =layer.load(1, {shade: [0.1,'#fff']});
                var id = this.value;
                var is_open = obj.elem.checked===true?1:0;

                $.post('{:url("engin/adminState")}',{'id':id,'is_open':is_open},function (res) {
                    layer.close(loading);
                    if (res.status==1) {
                        //tableIn.reload();
                        layer.msg('修改成功',{icon:6});
                    }else{
                        layer.msg(res.msg,{time:1000,icon:2});
                        return false;
                    }
                })
            });
            table.on('tool(list)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('{:lang("Are you sure you want to delete it")}', function(index){
                        $.post("{:url('Auth/adminDel')}",{admin_id:data.admin_id},function(res){
                            if(res.code==1){
                                layer.msg(res.msg,{time:1000,icon:1});
                                obj.del();
                            }else{
                                layer.msg(res.msg,{time:1000,icon:2});
                            }
                        });
                        layer.close(index);
                    });
                }else if(obj.event === 'resw'){
                    layer.confirm('确认将该工程师的工作状态重置为空闲吗？', function(index){
                        $.post("{:url('Engin/adminWork')}",{admin_id:data.admin_id},function(res){
                            if(res.code==1){
                                layer.msg(res.msg,{time:1000,icon:1});
                                han();
                            }else{
                                layer.msg(res.msg,{time:1000,icon:2});
                            }
                        });
                        layer.close(index);
                    });
                }else if(obj.event == 'gr'){
                    let ns = $(this).attr('ns');
                    layer.confirm(ns=='1'?'确认将该工程师设为组长吗？':'确认取消组长吗？', function(index){
                        $.post("{:url('Engin/isgroup')}",{admin_id:data.admin_id,is_group:ns},function(res){
                            if(res.code==1){
                                layer.msg(res.msg,{time:1000,icon:1},function () {
                                    han();
                                });
                            }else{
                                layer.msg(res.msg,{time:1000,icon:2});
                            }
                        });
                        layer.close(index);
                    });
                }
            });

        });
    }
    setTimeout(sl,1000);
    function sl(){
        //搜索
        $('#search').on('click', function () {
            var key = $('#key').val();
            if ($.trim(key) === '') {
                layer.msg('{:lang("pleaseEnter")}关键字！', {icon: 0});
                return;
            }
            var ck = wh;
            ck.key = key;
            ck.page = 1;
            tableIn.reload({
                where: ck
            });
        });
    }

</script>
</body>
</html>